package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.FreemarkerUtil;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.TravelLikeQueryObject;
import cn.wolfcode.trip.base.query.CartQueryObject;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.query.TravelCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService service;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ISignInService signInService;
    @Autowired
    private ICollectService collectService;

    @Autowired
    private IDailyService iDailyService;

    @Autowired
    private ITravelCollectService travelCollectService;
    @Autowired
    private ICartService cartService;
    @Autowired
    private ServletContext ctx;
    @Autowired
    private ITravelCommentService travelCommentService;
    @Autowired
    private ITravelLikeService travelLikeService;
    @Autowired
    private IAddressService addressService;

    @PostMapping()
    public Object register(User user) {
        JSONResult result = new JSONResult();
        try {
            service.register(user);
            result.setObj(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        result.setObj(user);
        return result;

    }

    @PutMapping("/{id}")
    public Object update(User user) {
        JSONResult result = new JSONResult();
        try {
            service.updateInfo(user);
            result.setObj(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }

    @GetMapping("/{userId}/travels")
    public Object queryTravels(TravelQueryObject qo) {
        qo.setOrderBy("lastUpdateTime desc");
        PageInfo<Travel> list = travelService.selectForList(qo);

        return list;
    }

    @GetMapping("/{id}/signIns")
    public Object getSignIns(@PathVariable Long id) {

        return signInService.listSignInTimeById(id);
    }

    //收藏日报
    @PostMapping("/collect")
    public Object insertCollect(Collect collect) {
        JSONResult jsonResult = new JSONResult();
        collectService.insertCollect(collect);
        return jsonResult;
    }

    //查询日报
    @GetMapping("/{id}/daily")
    public Object getDaily(DailyQueryObject qo) {
        qo.setPageSize(6);
        return iDailyService.getDaily(qo);
    }

    //查询某个用户收藏的日报ID用来回响
    @GetMapping("/{id}/dailyIds")
    public Object getListDailyIds(@PathVariable long id) {
        return collectService.listDailyIds(id);
    }

    //查询用户有没有收藏该点击的日报
    @GetMapping("/{id}/daily/collect")
    public Object queryUserDaily(@PathVariable long id, long dailyId) {
        JSONResult result = new JSONResult();
        Map<String, Object> map = collectService.queryUserDaily(id, dailyId);
        if (map == null) {
            result.mark("1");
        }
        return result;
    }


    //删除用户的收藏
    @DeleteMapping("/daily")
    public Object deleteDaily(Collect collect) {
        JSONResult jsonResult = new JSONResult();
        collectService.deleteDaily(collect);
        return jsonResult;
    }

    //收藏游记
    @PostMapping("/travel/collect")
    public Object insertTravelCollect(TravelCollect collect) {
        JSONResult jsonResult = new JSONResult();
        travelCollectService.insertCollect(collect);
        return jsonResult;
    }

    //查询收藏游记
    @GetMapping("/{id}/travel/collect")

    public Object gettravelDaily(DailyQueryObject qo) {
        if (qo.getTravelId() > -1) {
            JSONResult result = new JSONResult();
            PageInfo<Travel> daily = travelService.getDaily(qo);
            List<Travel> list = daily.getList();
            if (list.size() == 0) {
                result.mark("");
            }
            return result;

        }
        qo.setPageSize(6);
        return travelService.getDaily(qo);
    }

    //删除用户游记的收藏
    @DeleteMapping("/travel/collect")
    public Object deleteTravelDaily(TravelCollect collect) {
        JSONResult jsonResult = new JSONResult();
        travelCollectService.deleteDaily(collect);
        return jsonResult;
    }

    //删除用户日报的收藏
    @DeleteMapping("/daily/collect")
    public Object deleteDaily(long dailyId, long userId) {
        JSONResult jsonResult = new JSONResult();
        collectService.deleteDailyId(dailyId, userId);
        return jsonResult;
    }


    /*
       查询某个用户购物车里的信息
       资源:/users/{userId}/carts
       动作:GET
       参数:
    */
    @GetMapping("/{userId}/carts")
    public void listCarts(@PathVariable Long userId, HttpServletResponse resp) throws Exception {
        resp.setContentType("text/html;charset=utf-8");
        String dir = ctx.getRealPath("/template");
//         qo.setPageSize(2);
        Map<String, Object> map = new HashMap<>();
        List<Cart> carts = cartService.listUndeterminedCart(userId);
        map.put("info", carts);
//         map.put("id",qo.getType());
        FreemarkerUtil.wirteTempDate(dir, "cartClassify.ftl", map, resp.getWriter());

    }

     @GetMapping("/{userId}/record")
    public Object listRecord(CartQueryObject qo){
         qo.setPageSize(10);
         qo.setOrderBy("c.addTime desc");
        PageInfo<Cart> result = cartService.listRecord(qo);
        return result;
    }

    /*
       更新用户的积分---这里是消费的积分
       资源:/users/{id}/points
       动作: PUT
       参数:consume=xxx
     */
    @PutMapping("/{id}/points")
    public void updatePoint(@PathVariable Long id, Integer consume) {
        service.updatePoint(id, consume);
    }

     /*
        获取某个用户的所有收货地址
        资源: /users/{id}/addresses
        动作: GET
        参数
      */
     @GetMapping("/{id}/addresses")
     public Object listAddresses(@PathVariable Long id){
        return addressService.listAddresses(id);
     }
    /**
     * 保存用户点赞游记信息
     *
     * @param travelLike 用户点赞游记信息
     */
    @PostMapping("/{userId}/like")
    public void like(TravelLike travelLike) {
        travelLikeService.like(travelLike);
    }

    /**
     * 删除用户点赞信息
     *
     * @param travelLike
     */
    @DeleteMapping("/{userId}/dislike")
    public void dislike(TravelLike travelLike) {
        travelLikeService.delete(travelLike);
    }

    /**
     * 查询是否已点赞谋篇游记,如果有返回点赞信息
     *
     * @param travelLike
     * @return
     */
    @GetMapping("/{userId}/like")
    public Object isLike(TravelLike travelLike) {
        TravelLike like = travelLikeService.getLike(travelLike);
        return like;

    }


    //查询用户评论消息未读量
    @GetMapping("/{id}/commentNum")
    public Object getCommentNum(@PathVariable Long id) {
        return travelCommentService.CommentMeNumById(id);
    }

    //查询评论当前用户的评论信息
    @GetMapping("/{travelAuthorId}/commentMes")
    public Object getCommentMes(TravelCommentQueryObject qo) {
        qo.setPageSize(6);
        qo.setOrderBy("tc.createTime desc");
        return travelCommentService.listCommentMesInfo(qo);
    }

    //修改当前用户的评论信息状态
    @PutMapping("/{id}/commentMes")
    public void changeIsRead(@PathVariable Long id) {
        travelCommentService.changeIsReadById(id, true);
    }


    //查询用户点赞数量
    @GetMapping("/{id}/travelLikeNum")
    public Object getTravelLikeNum(@PathVariable Long id){
        return travelLikeService.getTravelLikeNum(id,false);
    }

    //修改用户点赞数量信息状态
    @PutMapping("/{id}/travelLikeIsRead")
    public void changeLikeIsRead(@PathVariable Long id){
        travelLikeService.changeLikeIsReadById(id,true);
    }

    //查询点赞我的用户
    @GetMapping("/{travelAuthorId}/travelLikeIMes")
    public Object getTravelLikeIMes(TravelLikeQueryObject qo){
        qo.setPageSize(7);
        qo.setOrderBy("tl.createTime desc");
        return travelLikeService.getTravelLikeIMes(qo);
    }
}
